<?php
namespace gzapi\train\controller;

use cmf\controller\PublicController;
use cmf\controller\BaseController;
use think\Validate;
use gzapi\common\business\Train;

class AzdController extends BaseController
{
    //方法验证参数
    protected $Train;
    protected $training_status = [
        '创建' => 1,
        '编辑' => 2,
        '提交' => 3,
    ];

    //输出签到页面
    public function sign()
    {
        $this->Train = new Train;
        //判断是否可以签到
        $get_data = input('param.');
        if(!isset($get_data['azd_id']) || !$get_data['azd_id'])
        {
            $this->error('信息错误');
        }
        if(!isset($get_data['train_id']) || !$get_data['train_id'])
        {
            $this->error('信息错误');
        }
        if(!isset($get_data['train_no']) || !$get_data['train_no'])
        {
            $this->error('信息错误');
        }
        //判断是否提交，是否在在开始培训时间之后（往前加一天）
        $where['training_no'] = $get_data['train_no'];
        $where['azd_id'] = $get_data['azd_id'];
        $where['row_id'] = $get_data['train_id'];
        $result = $this->Train->isSign($where);
        if($result)
        {
            include (APP_PATH.'/train/view/azd/404.html');
        }else{
            $this->assign('data',$get_data);
            include (APP_PATH.'/train/view/azd/sign.html');
        }

    }

    //提交培训人员信息
    public function azdSign()
    {
        $this->Train = new Train;
        $get_data = input('param.');
        if(!isset($get_data['azd_id']) || !$get_data['azd_id'])
        {
            $this->error('信息错误');
        }
        if(!isset($get_data['train_id']) || !$get_data['train_id'])
        {
            $this->error('信息错误');
        }
        if(!isset($get_data['train_no']) || !$get_data['train_no'])
        {
            $this->error('信息错误');
        }
        if(!isset($get_data['train_person_name']) || !$get_data['train_person_name'])
        {
            $this->error('请填写姓名');
        }
        if(!isset($get_data['train_person_phone']) || !$get_data['train_person_phone'] || !preg_match("/^1[3456789]\d{9}$/", $get_data['train_person_phone']))
        {
            $this->error('请填写手机号');
        }
        if(!isset($get_data['train_person_post']) || !$get_data['train_person_post'])
        {
            $this->error('请填写职位');
        }
        if(!isset($get_data['affiliated_units']) || !$get_data['affiliated_units'])
        {
            $this->error('请填写所属单位');
        }
        $data['azd_id'] = $get_data['azd_id'];
        $data['train_id'] = $get_data['train_id'];
        $data['train_person_name'] = $get_data['train_person_name'];
        $data['train_person_phone'] = $get_data['train_person_phone'];
        $data['train_person_post'] = $get_data['train_person_post'];
        $data['affiliated_units'] = $get_data['affiliated_units'];
        $data['remark'] = isset($get_data['remark'])?'':$get_data['remark'];
        $data['train_no'] = $get_data['train_no'];
        $data['created_date'] = date('Y-m-d',time());
        $where['train_person_name'] = $get_data['train_person_name'];
        $where['train_person_phone'] = $get_data['train_person_phone'];
        $where['train_no'] = $get_data['train_no'];
        $result = $this->Train->AzdSign($where,$data);
        if($result['code'])
        {
            $this->success("保存成功!");
        }else{
            $this->error($result['msg']);
        }
    }

    public function inspectionUploadPic()
    {


        // 获取上传文件
        $file = $this->request->file('file');
        $place = $this->request->param('place','是空的，没有值');
        // 判断图片是否存在
        if (empty($file)) {
            $this->error('请选择上传文件');
        }
        // 获取当前时间
        $date = date('Ymd',time());
        // 移动到框架应用根目录/uploads/ 目录下 3MB
        $info = $file->rule('md5')->move( '../uploads/inspect/'.$date);
        // 判断图片是否上传成功
        if($info){
            $list['path'] = 'http://'.$_SERVER['SERVER_NAME'] .'/uploads/inspect/'.$date.'/'.$info->getSaveName();
//            $this->success('操作成功！', $list);
            return json(['msg'=>'操作成功','path'=>$list['path'],'code'=>1]);
        }else{
            // 上传失败获取错误信息
            $this->error($file->getError());
        }
    }
}